#猜拳游戏

import random
win_times=0
while win_times <=2:
    player=int(input("请输入：剪刀(0) 石头(1) 布(2):"))

    computer = random.randint(0,2)

    if((player==0) and (computer==2)) or ((player==1) and (computer==0)) or ((player==3) and (computer == 1)):
        win_times +=1
        print(f'您已经获胜{win_times}局 继续加油')
        continue
    elif player==computer:
        print('平局 再来一局')
        continue
    else:
        print('您输了 洗洗手 决战到天亮')







#银行管理系统
import random


class User:
    def __init__(self, name, id, phone, card):
        self.name=name
        self.id=id
        self.phone=phone
        self.card=card


class Card:
    def __init__(self, cardId, cardPwd, money):
        self.cardId=cardId
        self.cardPwd=cardPwd
        self.money=money
        self.cardLock=False


class Admin:
    adminU='123'
    adpwd='123456'
    
    def printAdminView(self):
        print('***************************************************')
        print('***                                             ***')
        print('***            欢迎使用银行管理系统             ***')
        print('***                                             ***')
        print('***************************************************')



    def printFunctionView(self):
        print('****************************************************')
        print('***                                              ***')
        print('***      1.开户(1)               2.查询(2)       ***')
        print('***      3.取款(3)               4.存款(4)       ***')
        print('***      5.锁定(5)               6.解锁(6)       ***')
        print('***      7.转账(7)               0.退出(0)       ***')
        print('***                                              ***')
        print('****************************************************')
        

    def adminOption(self):
        adminInput = input('请输入管理员账户:')
        if adminInput != self.adminU :
            print('管理员账户输入错误......')
            return -1
        passwordInput = input('请输入密码:')
        if passwordInput != self.adpwd :
            print('输入密码有误......')
            return -1
        else :
            print('操作成功,请稍后.....')
            return 0    



class ATM:
    def __init__(self,alluser):
        self.alluser = alluser

    def randomCardId(self):     #随机生成开户卡号
        while True:
            str_data = ''
            for i in range(6):
                ch = chr(random.randrange(ord('0'), ord('9') + 1))
                str_data += ch
            if not self.alluser.get(str):
                return str_data


    def creatUser(self):    #开设账户
        name = input('请输入姓名:')
        id = input('请输入身份证号:')
        phone = input('请输入手机号:')
        prestMoney = float(input('请输入预存金额:'))
        if prestMoney <= 0:
            print('预存款输入有误,开户失败....')
            return -1
        oncePwd = input('请输入密码:')
        passWord = input('请再次输入密码:')
        if passWord != oncePwd :
            print('两次密码输入不同.....')
            return -1
        print('密码设置成功,请牢记密码: %s'%passWord)

        cardId = self.randomCardId()        #生成卡号
        card = Card(cardId, passWord, prestMoney)   #创建卡
        user = User(name, id, phone, card)          #创建用户
        self.alluser[cardId] = user                 #存入用户字典
        print('您的账户已开通,请牢记开户账号:%s'%cardId)



    def checkpwg(self, realPwd):    #核对密码
        for i in range(3):
            psd2 = input('请输入密码:')
            if realPwd == psd2:
                return True
            else:
                print(f'您还有{2-i}次机会')
        print('密码输错三次,系统自动退出......')
        return False
    


    def serachUser(self,base = 1):           #查询用户
        if base == 2:
            inptcardId = input('请输入转出卡号:')
        elif base == 3:
            inptcardId = input('请输入转入卡号:')
        elif base == 1:
            inptcardId = input('请输入您的卡号:')
        user = self.alluser.get(inptcardId)

        if not self.alluser.get(inptcardId):
            print('此卡号不存在...查询失败！')
            return -1
        
        if user.card.cardLock:
            print('该卡已经被锁定...请解卡后使用！')
            return -1
        
        if not self.checkpwg(user.card.cardPwd):
            print('密码错误过多...卡已经被锁定,请解卡后使用！！')
            user.card.cardLock = True
            return -1
        else:
            print('账户:%s   余额:%.2f' %(user.card.cardId,user.card.money))
            return user



    def lockCard(self):             #锁定银行卡
        inptcardId = input('请输入您的卡号:')
        user = self.alluser.get(inptcardId)

        if not self.alluser.get(inptcardId):
            print('此卡号不存在...锁定失败')
            return -1
        
        if user.card.cardLock:
            print('该卡已经被锁定 无需再次锁定!')
            return -1
        
        if not self.checkpwg(user.card.cardPwd):
            print('密码错误...锁定失败！！')
            return -1
        user.card.cardLock = True
        print('该卡被锁定成功！')
    


    def unlockCard(self):           #解锁银行卡
        inptcardId = input('请输入您的卡号:')
        user = self.alluser.get(inptcardId)
        while 1:
            if not self.alluser.get(inptcardId):
                print('此卡号不存在...解锁失败')
                return -1
            
            elif not user.card.cardLock:
                print("此卡未被锁定,无需解锁！")
                break

            elif not self.checkpwg(user.card.cardPwd):
                print('密码错误...解锁失败！！')
                return -1
            
            user.card.cardLock = False
            print('该卡解锁成功！')
            break



    def getMoney(self):             #取钱
        userTF = self.serachUser()
        if userTF != -1:
            if userTF.card.cardId != " ":
                inptMoney = float(input('请输入取款金额:'))
                if inptMoney > int(userTF.card.money):
                    print('您的余额不足 剩余余额为:%.2f'%userTF.card.money)
                    return -1
                userTF.card.money = float(userTF.card.money) - inptMoney
                print('取款成功！   账户: %s     金额: %.2f' %(userTF.card.cardId,userTF.card.money))
        else:
            return -1



    def saveMoney(self):            #存钱
        userTF = self.serachUser()
        if userTF != -1:
            if not userTF.card.cardLock == True:
                if userTF.card.cardId != ' ':
                    inptMoney = float(input('请输入存款金额:'))
                    if inptMoney < 0:
                        print('请输入正确金额')
                    else:
                        userTF.card.money = float(userTF.card.money) + inptMoney
                        print('存款成功！   账户: %s     金额: %.2f' %(userTF.card.cardId,userTF.card.money))

        else:
            return -1
        


    def transferMoney(self):        #转账
        MasterTF = self.serachUser(base=2)
        if (MasterTF == -1):
            return -1
        
        userTF = self.serachUser(base=3)
        if (userTF == -1):
            return -1
        
        in_tr_Money = float(input('请输入转账金额:'))

        if MasterTF.card.money >= in_tr_Money:
            str = input('请确认要继续您的操作吗(y/n):')
            if str.upper() == 'y':
                MasterTF.card.money -= in_tr_Money
                print('转账成功！')
            else :
                print('您已经取消操作！')
        else:
            print('转账失败,余额不足,剩余余额为:%.2f'%MasterTF.card.money)



class HomePage:
    def __init__(self):
        self.allUserD = {}
        self.atm = ATM(self.allUserD)
        self.admin = Admin()

    def saveUser(self):
        self.allUserD.update(self.atm.alluser)
        print('数据存储成功')


    def main(self):
        self.admin.printAdminView()
        resL = self.admin.adminOption()
        if not resL:
            while True:
                self.admin.printFunctionView()
                option = input('请输入您的操作:')
                if option not in ('1','2','3','4','5','6','7','0'):
                    print('输入操作项有误，请仔细确认！')


                if option == '1':       #开户
                    self.atm.creatUser()
                    
                elif option == '2':     #查询
                    self.atm.serachUser()

                elif option == '3':     #取款
                    self.atm.getMoney()

                elif option == '4':     #存款
                    self.atm.saveMoney()

                elif option == '5':     #锁定
                    self.atm.lockCard()
                
                elif option == '6':     #解锁
                    self.atm.unlockCard()

                elif option == '7':     #转账
                    self.atm.transferMoney()

                elif option == '0':
                    if not (self.admin.adminOption()):
                        self.saveUser()
                        print('退出系统')
                        return -1


homepage= HomePage()
homepage.main()




#井字棋




import random  
  
class Board:  
    def __init__(self):  
        self.board = [[' ' for _ in range(3)] for _ in range(3)]  
  
    def show_board(self):  
        print("  0 1 2")  
        for i, row in enumerate(self.board):  
            print(f"{i} {' '.join(row)}")  
  
    def get_position_name(self, row, col):  
        return f"{row}{col}"  
  
    def is_position_valid(self, row, col):  
        return 0 <= row < 3 and 0 <= col < 3 and self.board[row][col] == ' '  
  
    def place_marker(self, row, col, marker):  
        self.board[row][col] = marker  
  
    def check_win(self, marker):  
        # Check rows  
        for row in self.board:  
            if all(cell == marker for cell in row):  
                return True  
        # Check columns  
        for col in range(3):  
            if all(self.board[row][col] == marker for row in range(3)):  
                return True  
        # Check diagonals  
        if all(self.board[i][i] == marker for i in range(3)):  
            return True  
        if all(self.board[i][2-i] == marker for i in range(3)):  
            return True  
        return False  
  
    def is_draw(self):  
        return all(cell != ' ' for row in self.board for cell in row)  
  
        
    def get_empty_positions(self):  
        return [(i, j) for i in range(3) for j in range(3) if self.board[i][j] == ' ']  
  
  


class Player:  
    def __init__(self, name, marker):  
        self.name = name  
        self.marker = marker  
        self.score = 0  
  
    def place_marker(self, board, row, col):  
        board.place_marker(row, col, self.marker)  
  
  




class AIPlayer(Player):  

    def place_marker(self, board, human_marker):  
        empty_positions = board.get_empty_positions()  
          
        # Check for winning move  
        for pos in empty_positions:  
            row, col = pos  
            board.place_marker(row, col, self.marker)  
            if board.check_win(self.marker):  
                return pos  
            board.place_marker(row, col, ' ')  
          
        # Block losing move  
        for pos in empty_positions:  
            row, col = pos  
            board.place_marker(row, col, human_marker)  
            if board.check_win(human_marker):  
                board.place_marker(row, col, ' ')  
                board.place_marker(row, col, self.marker)  
                return pos  
            board.place_marker(row, col, ' ')  
          
        # Evaluate positions based on strategy  
        center_position = (1, 1)  
        corner_positions = [(0, 0), (0, 2), (2, 0), (2, 2)]  
        edge_positions = [(0, 1), (1, 0), (1, 2), (2, 1)]  
          
        if center_position in empty_positions:  
            return center_position  
        elif any(pos in empty_positions for pos in corner_positions):  
            return random.choice([pos for pos in corner_positions if pos in empty_positions])  
        elif any(pos in empty_positions for pos in edge_positions):  
            return random.choice([pos for pos in edge_positions if pos in empty_positions])  
  
  



class Game:  
    def __init__(self):  
        self.board = Board()  
        self.human_player = Player("Human", 'O')  
        self.ai_player = AIPlayer("AI", 'X')  
        self.human_score = 0  
        self.ai_score = 0  
  
    def start_game(self):  
        while True:  
            self.board.show_board()  
            current_player = self.human_player if self.board.get_empty_positions() else None  
              
            if current_player == self.human_player:  
                row = int(input("Enter row (0, 1, 2): "))  
                col = int(input("Enter col (0, 1, 2): "))  
                  
                while not self.board.is_position_valid(row, col):  
                    print("Invalid position. Try again.")  
                    row = int(input("Enter row (0, 1, 2): "))  
                    col = int(input("Enter col (0, 1, 2): "))  
                  
                self.human_player.place_marker(self.board, row, col)  
                  
                if self.board.check_win(self.human_player.marker):  
                    print(f"{self.human_player.name} wins!")  
                    self.human_score += 1  
                    break  
                elif self.board.is_draw():  
                    print("该位置已经被占领!")  
                    break  
                else:  
                    current_player = self.ai_player  
              
            if current_player == self.ai_player:  
                pos = self.ai_player.place_marker(self.board, self.human_player.marker)  
                row, col = pos  
                print(f"{self.ai_player.name} places marker at {self.board.get_position_name(row, col)}")  
                  
                if self.board.check_win(self.ai_player.marker):  
                    print(f"{self.ai_player.name} wins!")  
                    self.ai_score += 1  
                    break  
                elif self.board.is_draw():  
                    print("It's a draw!")  
                    break  
  
            self.board.show_board()  
  
        print(f"Final Score: Human - {self.human_score}, AI - {self.ai_score}")  
  
  


  
if __name__ == "__main__":  
    game = Game()  
    game.start_game()
